import util from '@/libs/util';
import Cookies from 'js-cookie';
import studentList from './studentList.vue';
import customTree from './customTree.vue';
import applyclasstime from '../components/applyclasstime/applyclasstime.vue';
import orderdetails from '../components/orderDetails/orderdetails.vue';

export default {
    components: {
        studentList, customTree, applyclasstime, orderdetails
    },
    data () {
        return {
            columns: [{
                title: '班级名称 ',
                key: 'name'
            }, {
                title: '专业',
                key: 'marjorName'
            }, {
                title: '负责教师',
                key: 'mainTeacherName'
            }, {
                title: '开课时间',
                key: 'classStarttime',
                render: (h, params) => {
                    return h('div', {}, util.getDate(params.row.classStarttime, 'date'));
                }
            }, {
                title: '结束时间',
                key: 'classEndtime',
                render: (h, params) => {
                    return h('div', {}, util.getDate(params.row.classEndtime, 'date'));
                }
            }, {
                title: '教材费(元)',
                key: 'money',
                render: (h, params) => {
                    return h('div', {}, params.row.otherCost);
                    // + params.row.teachCost
                }
            }, {
                title: '每课次费(元)',
                key: 'money',
                render: (h, params) => {
                    return h('div', {}, params.row.teachCost);
                }
            }, {
                title: '总费用(元)',
                key: 'totalAmt',
                render: (h, params) => {
                    console.log(this.selectClassTime);
                    let total = parseFloat(this.selectClassTime) * parseFloat(params.row.teachCost) + parseFloat(params.row.otherCost);
                    return h('span', {}, total);
                }
            }, {
                title: '预招(预留)人数',
                key: 'initiallyPeople',
                render: (h, params) => {
                    return h('div', {}, params.row.initiallyPeople + '(' + params.row.reservedPeople + ')');
                }
            }, {
                title: '报名人数',
                key: 'applyNo'
            }, {
                title: '缴费人数',
                key: 'chargeNo'
            }, {
                title: '课程安排',
                key: 'classSchedules',
                width: 180,
                render: function (h, params) {
                    let list = params.row.classSchedules;
                    if (typeof (list) === 'string') {
                        list = JSON.parse(list);
                    }
                    let list_h = [];
                    for (let i in list) {
                        let startTime = list[i].classStartTime;
                        let endTime = list[i].classEndTime;
                        if (startTime) {
                            startTime = startTime.substring(0, 2) + ':' + startTime.substring(2, 4);
                        }
                        if (endTime) {
                            endTime = endTime.substring(0, 2) + ':' + endTime.substring(2, 4);
                        }
                        let fun = h('div', util.convertNumToWeekOfDay(list[i].classType) + ' ' + startTime + '-' + endTime + ' ' + list[i].classRoomName);
                        list_h.push(fun);
                    }
                    return h('span', list_h);
                }
            }, {
                title: '班级状态',
                key: 'status',
                render: (h, params) => {
                    let text = '';
                    if (params.row.status === 1) text = '草稿';
                    if (params.row.status === 2) text = '核审通过';
                    if (params.row.status === 3) text = '核审未通过';
                    return h('div', {}, text);
                }
            }, {
                title: '操作',
                key: 'action',
                width: 130,
                align: 'center',
                render: (h, params) => {
                    return h('div', [
                        // h('Button', {
                        //     props: {
                        //         type: 'primary',
                        //         size: 'small'
                        //     },
                        //     style: {
                        //         marginRight: '5px'
                        //     },
                        //     on: {
                        //         click: () => {
                        //             this.detail(params.row.id);
                        //         }
                        //     }
                        // }, '查看'),
                        h('Button', {
                            props: {
                                type: 'primary',
                                size: 'small'
                            },
                            style: {
                                marginRight: '5px'
                            },
                            on: {
                                click: () => {
                                    let selectClassTime = this.selectClassTime;
                                    if (selectClassTime == null || selectClassTime == '' || selectClassTime == undefined || selectClassTime == 0) {
                                        this.$Message.warning('请选择课次');
                                    } else if (!this.formData.classStartTime) {
                                        this.$Message.warning('请选择开课时间');
                                    } else {
                                        this.signup(params.row);
                                    }
                                }
                            }
                        }, '报名'),
                        h('Button', {
                            props: {
                                type: 'primary',
                                size: 'small'
                            },
                            style: {
                                marginRight: '5px'
                            },
                            on: {
                                click: () => {
                                    let selectClassTime = this.selectClassTime;
                                    util.ajax.post('/busine/class/time/getClassId', util.toFormData({
                                        classId: params.row.id
                                    })).then(res => {
                                        if (res.data.code == 200) {
                                            let data = res.data.data;
                                            this.$refs.applyclasstime.classTimeList = data;
                                        }
                                    });
                                    this.applyClassTimeVisible = true;
                                }
                            }
                        }, '选定课次')
                    ]);
                }
            }],
            baseuser: JSON.parse(Cookies.get('baseUser')),
            data: [],
            formData: {
                isEat: 'YES',
                chargeValidity: new Date(((new Date()).getTime() + (24 * 60 * 60 * 1000)))
            },
            query: {},
            selectYesOrNo: [{id: 'YES', name: '是'}, {id: 'NO', name: '否'}],
            sexList: [{id: 'MAN', name: '男'}, {id: 'WOMAN', name: '女'}],
            gradeList: [],
            LargeMajorList: [],
            majorList: [],
            semesterList: [],
            applyList: [],

            total: 0,
            pageIndex: 1,
            pageSize: 5,

            studentModal: false,
            isRead: false,
            deptModal: false,
            deltailModal: false,
            applyClassTimeVisible: false,
            selectClassTime: '',
            feeVisible: false
        };
    },
    mounted () {
        this.loadDictionaries();
    },
    methods: {
        loadDictionaries () {
            util.ajax.post('/base/datadictionary/findByCode', util.toFormData({
                CODE: 'GRADE'
            })).then(res => {
                this.gradeList = res.data.data;
            });
            util.ajax.post('/base/datadictionary/findByCode', util.toFormData({
                CODE: 'CLASS_SEASON'
            })).then(res => {
                this.semesterList = res.data.data;
            });
            util.ajax.post('/base/datadictionary/findByCode', util.toFormData({
                CODE: 'CLASS_APPLY_TYPE'
            })).then(res => {
                this.applyList = res.data.data;
            });
            util.ajax.post('/busine/major/findParent', util.toFormData({
                organizeid: this.baseuser.orgId
            })).then(res => {
                this.LargeMajorList = res.data.data;
            });
        },
        findByParent (parentid) {
            util.ajax.post('/busine/major/findByParent', util.toFormData({
                parentid: parentid
            })).then(res => {
                this.majorList = res.data.data;
            });
        },
        loadList () {
            util.ajax.post('/base/class/list', util.toFormData({
                page: this.pageIndex,
                size: this.pageSize,
                organizeId: this.baseuser.orgId,
                name: this.query.name ? this.query.name : '',
                year: this.query.year ? this.query.year : '',
                classSeason: this.query.classSeason ? this.query.classSeason : '',
                departmentId: this.query.departmentId ? this.query.departmentId : '',
                marjorId: this.query.marjorId ? this.query.marjorId : '',
                releaseStatus: 2
            })).then(res => {
                this.data = res.data.data.list;
                this.total = res.data.data.total;
            });
        },
        refresh () {
            this.query = {};
            this.loadList();
        },
        setStudent () {
            this.isRead = true;
            this.formData = this.$refs.student.student;
            this.formData.isEat = 'YES';
            this.formData.chargeValidity = new Date(((new Date()).getTime() + (24 * 60 * 60 * 1000)));
        },
        resetStudent () {
            this.isRead = false;
            this.formData = {
                isEat: 'YES',
                chargeValidity: new Date(((new Date()).getTime() + (24 * 60 * 60 * 1000)))
            };
        },
        setDept () {
            if (this.$refs.dept.selectData[0]) {
                this.query.departmentId = this.$refs.dept.selectData[0].id;
                this.query.departmentName = this.$refs.dept.selectData[0].title;
            } else {
                this.query.departmentId = '';
                this.query.departmentName = '';
            }
            this.deptModal = false;
        },
        cancelSelect () {
            this.$refs.dept.cancelSelect();
        },
        formValidate () {
            let boo = false;
            let content = '';
            if (!this.formData.parentphone) {
                boo = true;
                content = '家长手机号不能为空';
            }
            // if (!this.formData.birthdate) {
            //     boo = true;
            //     content = '出生日期不能为空';
            // }
            if (!this.formData.name) {
                boo = true;
                content = '学生姓名不能为空';
            }
            if (boo) {
                this.$Modal.error({
                    content: content
                });
                return false;
            }
            return true;
        },
        signup: function (obj) {
            if (!this.formValidate()) return false;
            let formData = {};
            for (var key in this.formData) {
                formData[key] = this.formData[key];
            }
            formData.birthdate = util.getDate(formData.birthdate, 'date');
            formData.chargeValidity = util.getDate(formData.chargeValidity, 'time');
            formData.issignup = 'YES';
            formData.organizeid = this.baseuser.orgId;
            util.ajax.post('/busine/student/save', util.toFormData(formData)).then(res => {
                if (res.data.message === 'SUCCESS') {
                    util.ajax.post('/busine/bill/list', util.toFormData({
                        classid: obj.id,
                        studentid: res.data.data.id
                    })).then(listRes => {
                        if (listRes.data.data.list.length !== 0) {
                            this.$Modal.error({
                                content: '已报名，请勿重新报名！'
                            });
                            return false;
                        } else {
                            let formBill = {};
                            formBill.classId = obj.id;
                            formBill.studentId = res.data.data.id;
                            formBill.signtype = 0;
                            formBill.classTime = this.$refs.applyclasstime.selectClassTimeSeted;
                            formBill.classStartTime = util.getDate(this.formData.classStartTime, 'date');

                            util.ajax.post('/busine/bill/doApply', util.toFormData(formBill)).then(billRes => {
                                if (billRes.data.message === 'SUCCESS') {
                                    this.$Message.success('报名成功！订单编号' + billRes.data.data.billcode);
                                    this.feeVisible = true;
                                    this.billId = billRes.data.data.id;
                                } else {
                                    this.$Modal.error({
                                        title: '警告',
                                        content: '出错了'
                                    });
                                }
                            });
                        }
                    });
                } else {
                    this.$Modal.error({
                        title: '警告',
                        content: '出错了'
                    });
                }
            });
        },
        detail (id) {
            this.deltailModal = true;
        },
        whenSetClassTime: function () {
            this.selectClassTime = this.$refs.applyclasstime.selectClassTimeSeted;
        },
        queryStudentByName: function (event) {
            let name = this.formData.name;
            util.ajax.post('/busine/student/detailsByName', util.toFormData({
                name: name
            })).then(res => {
                if (res.data.code == 200) {
                    this.$refs.student.student = res.data.data;
                    this.setStudent();
                }
            });
        }
    }
};
